package com.ithao.controller;

import com.ithao.common.GlobalConst;
import com.ithao.entity.Result;
import com.ithao.pojo.ReviewLog;
import com.ithao.pojo.User;
import com.ithao.service.ReviewLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;


/**
 * @author : ZhangHao
 * @version :1.0
 * @date :Created in 2020/3/30
 * @description :试题审核控制器
 */
@RestController
@Slf4j
@RequestMapping("/review")
public class ReviewController {

    @Autowired
    private ReviewLogService reviewLogService;

    /**
     * 添加审核记录
     *
     * @param reviewLog
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody ReviewLog reviewLog, HttpServletRequest request) {
        log.info("[审核] reviewLog:{}", reviewLog);
        User user = (User) request.getSession().getAttribute(GlobalConst.SESSION_KEY_USER);
        // 从上下文获取用户ID，调试默认为1
        reviewLog.setUserId(user != null ? user.getId() : 1);
        reviewLogService.add(reviewLog);
        if (reviewLog.getStatus() == 1) {
            return new Result(true, "已审核,并上架成功");
        } else
            return new Result(true, "已审核,拒绝上架");
    }
}
